package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.domain.*;
import cn.itsource.ymcc.feignclient.LoginFeignClient;
import cn.itsource.ymcc.mapper.UserBaseInfoMapper;
import cn.itsource.ymcc.mapper.UserMapper;
import cn.itsource.ymcc.mapper.UserRealInfoMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IUserService;
import cn.itsource.ymcc.util.AssertUtil;
import cn.itsource.ymcc.util.BitStatesUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.util.Password;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author zongchangjiang
 * @since 2023-03-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private UserBaseInfoMapper userBaseInfoMapper;
    @Autowired
    private UserRealInfoMapper realInfoMapper;
    @Autowired
    private LoginFeignClient loginFeignClient;

    @GlobalTransactional
    @Override
    public JsonResult register(RegisterVo registerVo) {
//        1. 基本参数校验  jsr303已经帮我做了
//        2. 手机验证码校验
        String key = registerVo.getMobile()+ Constants.REGISTER;
        AssertUtil.isTrue(redisTemplate.hasKey(key),"短信验证码已过期");
        String o = (String)redisTemplate.opsForValue().get(key);
        AssertUtil.isTrue(o.equalsIgnoreCase(registerVo.getSmsCode()),"短信验证码错误");
//        3. 手机是否已经被注册校验【需要通过手机号查询数据库】
        User phone = baseMapper.selectOne(new QueryWrapper<User>().eq("phone", registerVo.getMobile()));
        AssertUtil.isNull(phone,"手机号已被注册，请登录!"); //必须要自己去  悟
//        4. 远程保存login登录信息[Feign远程调用]  同步数据
        Login login = new Login();
        login.setUsername(registerVo.getMobile());
        //密码加密
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(registerVo.getPassword());
        login.setPassword(encode);
        login.setType(registerVo.getRegChannel());
        JsonResult jsonResult = loginFeignClient.saveOrUpdate(login);
        Long loginId = ((Integer) jsonResult.getData()).longValue();
//        5. 保存user注册信息
        User user = new User();
        user.setPhone(registerVo.getMobile());
        user.setNickName(registerVo.getMobile());
        user.setSecLevel(1);
        //注册成功
        Long bitState = BitStatesUtils.addState(0L, BitStatesUtils.OP_REGISTED);
        //手机认证成功   十进制的值
        bitState = BitStatesUtils.addState(bitState, BitStatesUtils.OP_AUTHED_PHONE);
        user.setBitState(bitState);
        user.setLoginId(loginId);
        baseMapper.insert(user); //mybatisPlus 单表不需要些sql 不知道用户什么时候要主键或者是不要主键 所有默认全都有
//        6. 保存user_base_info基本信息
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setRegChannel(registerVo.getRegChannel());
        userBaseInfo.setUserId(user.getId());
        userBaseInfoMapper.insert(userBaseInfo);
//        7. 保存t_user_real_info实名认证信息
        UserRealInfo userRealInfo = new UserRealInfo();
        userRealInfo.setUserId(user.getId());
        userRealInfo.setState(1);//待审核
        realInfoMapper.insert(userRealInfo);
//        8. 删除Redis中的手机验证码
        redisTemplate.delete(key);

//        int i = 1/0;


        return JsonResult.success();
    }

}
